(* main.ml : teste des ensembles *)
 
let tested = ref false
 
(* Strings *)
module StrSet = MySet.Make(String)
 
let randStr n =
  let rchar () =
    Char.chr ((Char.code 'a') + Random.int 26)
  in
  let s = String.create n in
    for i=0 to n-1 do
      s.[i] <- rchar ()
    done;
    s
 
let testString n =
  let m = 1 in
  let rec build = function
    | 0 -> StrSet.empty
    | k -> StrSet.add (randStr m) (build (k-1))
  in
  let s1 = build n and s2 = build n in
  let s = StrSet.union s1 s2 in
    begin
      Printf.printf "s1(%d) = {" (StrSet.cardinal s1);
      StrSet.iter (fun s -> Printf.printf " %S;" s) s1;
      Printf.printf "}\n%!";
      Printf.printf "s2(%d) = {" (StrSet.cardinal s2);
      StrSet.iter (fun s -> Printf.printf " %S;" s) s2;
      Printf.printf "}\n%!";
      if (StrSet.sub s1 s) then
	Printf.printf "s1 est sous ensemble de s\n%!";
      if (StrSet.sub s2 s) then
	Printf.printf "s2 est sous ensemble de s\n%!";
      Printf.printf "s(%d) = {" (StrSet.cardinal s);
      StrSet.iter (fun s -> Printf.printf " %S;" s) s;
      Printf.printf "}\n\n%!";
      tested := true;
    end
 
(* paires d'entier *)
module Pair =
struct
  type t = int * int
  let compare a b = Pervasives.compare a b
end
 
module PairSet = MySet.Make (Pair)
 
let randPair () = (Random.int 8, Random.int 8)
 
let pairs2string (a,b) =
  Printf.sprintf "(%d,%d)" a b
 
let testPairs n =
  let rec build = function
    | 0 -> PairSet.empty
    | k -> PairSet.add (randPair ()) (build (k-1))
  in
  let s1 = build n and s2 = build n in
  let s = PairSet.union s1 s2 in
    begin
      Printf.printf "s1(%d) = {" (PairSet.cardinal s1);
      PairSet.iter (fun p -> Printf.printf " %s;" (pairs2string p)) s1;
      Printf.printf "}\n%!";
      Printf.printf "s2(%d) = {" (PairSet.cardinal s2);
      PairSet.iter (fun p -> Printf.printf " %s;" (pairs2string p)) s2;
      Printf.printf "}\n%!";
      if (PairSet.sub s1 s) then
	Printf.printf "s1 est sous ensemble de s\n%!";
      if (PairSet.sub s2 s) then
	Printf.printf "s2 est sous ensemble de s\n%!";
      Printf.printf "s(%d) = {" (PairSet.cardinal s);
      PairSet.iter (fun p -> Printf.printf " %s;" (pairs2string p)) s;
      Printf.printf "}\n\n%!";
      tested := true;
    end
 
(* main *)
 
let ops = Arg.align [
  ("-seed", Arg.Int (Random.init), " choix de la graine, doit etre passe en premier");
  ("-str", Arg.Int testString, " test sur les strings");
  ("-pair", Arg.Int testPairs, " test sur les pairs")
]
 
let annon _ = ()
 
let main () =
  begin
    let _ = Random.self_init () in
    let usage =
      Printf.sprintf
	"%s [-str n | -int n] test les ensembles."
	(Sys.argv.(0))
    in
      Arg.parse ops annon usage;
      if not !tested then
	Arg.usage ops usage;
      exit (0);
  end
 
let _ = main ()

